{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Getting Started with LlamaParse: Parsing Modes Overview\n",
    "\n",
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_cloud_services/blob/main/examples/parse/parsing_modes/demo_presets.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
    "\n",
    "This notebook demonstrates the different parsing modes available in LlamaParse and how to use them effectively for document processing. We'll walk through three main parsing modes:\n",
    "\n",
    "1. **Cost-Effective Mode** (`parse_page_with_llm`) - Fast and economical parsing\n",
    "2. **Agentic Mode** (`parse_page_with_agent` with `gpt-4-1-mini`) - Enhanced parsing with agent capabilities (Default)\n",
    "3. **Agentic Plus Mode** (`parse_page_with_agent` with `anthropic-sonnet-4.0`) - Premium parsing with advanced models\n",
    "\n",
    "We'll use two sample documents:\n",
    "- Apple 2021 10-K filing (text-heavy financial document)\n",
    "- GenAI Research Report (visual-rich document with charts and diagrams)\n",
    "\n",
    "Status:\n",
    "| Last Executed | Version | State      |\n",
    "|---------------|---------|------------|\n",
    "| Aug-18-2025   | 0.6.61  | Maintained |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install \"llama-index>=0.13.0<0.14.0\" llama-cloud-services"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's set up our environment and initialize the necessary components."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from llama_cloud_services import LlamaParse\n",
    "from llama_index.llms.openai import OpenAI\n",
    "\n",
    "# Environment Variables - Make sure these are set\n",
    "# os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-...\"  # Set in environment\n",
    "# os.environ[\"OPENAI_API_KEY\"] = \"sk-proj-...\"   # Set in environment\n",
    "\n",
    "# Initialize LLM for question answering\n",
    "llm = OpenAI(model=\"gpt-5-mini\")\n",
    "\n",
    "# Project Configuration - Replace with your actual values\n",
    "project_id = \"<project_id>\"  # Replace with your project ID\n",
    "organization_id = \"<organization_id>\"  # Replace with your organization ID"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Document Files\n",
    "\n",
    "First, let's download our sample documents:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# Create data directory if it doesn't exist\n",
    "os.makedirs(\"data\", exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget \"https://s2.q4cdn.com/470004039/files/doc_financials/2021/q4/_10-K-2021-(As-Filed).pdf\" -O data/apple_2021_10k.pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget \"https://www.sas.com/content/dam/SAS/documents/marketing-whitepapers-ebooks/ebooks/en/generative-ai-global-research-report-113914.pdf\" -O data/genai_research_report.pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set file paths\n",
    "apple_10k_path = \"./data/apple_2021_10k.pdf\"\n",
    "genai_report_path = \"./data/genai_research_report.pdf\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Cost-Effective Mode\n",
    "\n",
    "The cost-effective mode (`parse_page_with_llm`) is ideal for:\n",
    "- High-volume document processing\n",
    "- Text-heavy documents without complex layouts\n",
    "- Budget-conscious applications\n",
    "\n",
    "This mode provides fast, economical parsing while maintaining good quality for standard documents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cost-Effective Mode Parser initialized\n"
     ]
    }
   ],
   "source": [
    "# Initialize Cost-Effective Mode Parser\n",
    "cost_effective_parser = LlamaParse(\n",
    "    parse_mode=\"parse_page_with_llm\",\n",
    "    high_res_ocr=True,\n",
    "    adaptive_long_table=True,\n",
    "    outlined_table_extraction=True,\n",
    "    output_tables_as_HTML=False,\n",
    "    result_type=\"markdown\",\n",
    "    project_id=project_id,\n",
    "    organization_id=organization_id,\n",
    ")\n",
    "\n",
    "print(\"Cost-Effective Mode Parser initialized\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parse Apple 10-K with Cost-Effective Mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parsing Apple 10-K with Cost-Effective Mode...\n",
      "Started parsing the file under job_id 2b27d681-7ea2-42c6-8925-4276c02a7efa\n",
      "Number of pages extracted: 82\n",
      "\n",
      "=== Sample Output - Page 32 (Cost-Effective Mode) ===\n",
      "Apple Inc.\n",
      "# CONSOLIDATED STATEMENTS OF OPERATIONS\n",
      "\n",
      "(In millions, except number of shares which are reflected in thousands and per share amounts)\n",
      "\n",
      "|                                              | Years ended                         | September 25, 2021 | September 26, 2020 | September 28, 2019 |\n",
      "| -------------------------------------------- | ----------------------------------- | ------------------ | ------------------ | ------------------ |\n",
      "| Net sales:                                   | Products                            | $297,392           | $220,747           | $213,883           |\n",
      "|                                              | Services                            | $68,425            | $53,768            | $46,291            |\n",
      "|                                              | Total net sales                     | $365,817           | $274,515           | $260,174           |\n",
      "| Cost of sales:                               | Products                            | $192,266           | $151,286           | $144,996           |\n",
      "|                                              | Services                            | $20,715            | $18,273            | $16,786            |\n",
      "|                                              | Total cost of sales                 | $212,981           | $169,559           | $161,782           |\n",
      "|                                              | Gross margin                        | $152,836           | $104,956           | $98,392            |\n",
      "| Operating expenses:                          | Research and development            | $21,914            | $18,752            | $16,217            |\n",
      "|                                              | Selling, general and administrative | $21,973            | $19,916            | $18,245            |\n",
      "|                                              | Total operating expenses            | $43,887            | $38,668            | $34,462            |\n",
      "| Operating income                             |                                     | $108,949           | $66,288            | $63,930            |\n",
      "| Other income/(expense), net                  |                                     | $258               | $803               | $1,807             |\n",
      "| Income before provision for income taxes     |                                     | $109,207           | $67,091            | $65,737            |\n",
      "| Provision for income taxes                   |                                     | $14,527            | $9,680             | $10,481            |\n",
      "| Net income                                   |                                     | $94,680            | $57,411            | $55,256            |\n",
      "| Earnings per share:                          | Basic                               | $5.67              | $3.31              | $2.99              |\n",
      "|                                              | Diluted                             | $5.61              | $3.28              | $2.97              |\n",
      "| Shares used in computing earnings per share: | Basic                               | 16,701,272         | 17,352,119         | 18,471,336         |\n",
      "|                                              | Diluted                             | 16,864,919         | 17,528,214         | 18,595,651         |\n",
      "\n",
      "See accompanying Notes to Consolidated Financial Statements.\n",
      "\n",
      "Apple Inc. | 2021 Form 10-K | 29\n"
     ]
    }
   ],
   "source": [
    "# Parse the Apple 10-K document\n",
    "print(\"Parsing Apple 10-K with Cost-Effective Mode...\")\n",
    "apple_result_cost_effective = await cost_effective_parser.aparse(apple_10k_path)\n",
    "\n",
    "# Get markdown nodes\n",
    "apple_nodes_cost_effective = apple_result_cost_effective.get_markdown_nodes(\n",
    "    split_by_page=True\n",
    ")\n",
    "print(f\"Number of pages extracted: {len(apple_nodes_cost_effective)}\")\n",
    "\n",
    "# Display sample output from page 32 (contains Q3 financial data)\n",
    "print(\"\\n=== Sample Output - Page 32 (Cost-Effective Mode) ===\")\n",
    "print(apple_nodes_cost_effective[31].text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Agentic Mode (Default)\n",
    "\n",
    "The agentic mode (`parse_page_with_agent` with `gpt-4-1-mini`) is the recommended default mode that offers:\n",
    "- Enhanced understanding of document structure\n",
    "- Better handling of complex layouts and tables\n",
    "- Improved extraction of visual elements\n",
    "- Balanced performance and cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Agentic Mode Parser initialized\n"
     ]
    }
   ],
   "source": [
    "# Initialize Agentic Mode Parser\n",
    "agentic_parser = LlamaParse(\n",
    "    parse_mode=\"parse_page_with_agent\",\n",
    "    model=\"openai-gpt-4-1-mini\",\n",
    "    high_res_ocr=True,\n",
    "    adaptive_long_table=True,\n",
    "    outlined_table_extraction=True,\n",
    "    output_tables_as_HTML=False,\n",
    "    result_type=\"markdown\",\n",
    "    project_id=project_id,\n",
    "    organization_id=organization_id,\n",
    ")\n",
    "\n",
    "print(\"Agentic Mode Parser initialized\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parse GenAI Research Report with Agentic Mode\n",
    "\n",
    "This document contains charts and visual elements, making it ideal for demonstrating the agentic mode's capabilities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parsing GenAI Research Report with Agentic Mode...\n",
      "Started parsing the file under job_id 3bef1c04-e18c-40d5-bc99-512ce045f035\n",
      ".Number of pages extracted: 38\n",
      "\n",
      "=== Sample Output - Page 7 (Agentic Mode) ===\n",
      "\n",
      "Only one in 10 businesses has undergone the preparation needed to comply with current and upcoming regulations concerning GenAI.  \n",
      "The majority of organizations lack a comprehensive governance framework for both AI and GenAI (seven in 10 adopters admit to this).\n",
      "\n",
      "| How prepared is your organization to comply with current and upcoming regulations concerning GenAI? | How prepared is your organization to comply with current and upcoming regulations concerning GenAI? | How prepared is your organization to comply with current and upcoming regulations concerning GenAI? | How prepared is your organization to comply with current and upcoming regulations concerning GenAI? | How prepared is your organization to comply with current and upcoming regulations concerning GenAI? |\n",
      "| --------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |\n",
      "| Fully prepared                                                                                      | 10% (All respondents using/planning to use GenAI)                                                   | 35% (Using GenAI and have fully implemented it)                                                     | 11% (Using GenAI but haven't yet fully implemented it)                                              | 3% (Not yet using GenAI but intend to within the next two years)                                    |\n",
      "| Moderately prepared                                                                                 | 48%                                                                                                 | 49%                                                                                                 | 66%                                                                                                 | 28%                                                                                                 |\n",
      "| Slightly prepared                                                                                   | 40%                                                                                                 | 15%                                                                                                 | 23%                                                                                                 | 64%                                                                                                 |\n",
      "| Not prepared                                                                                        | 2%                                                                                                  | 0%                                                                                                  | 0%                                                                                                  | 5%                                                                                                  |\n",
      "\n",
      "\n",
      "<p><i>Please note that percentages on charts may not add to 100% due to rounding</i></p>\n",
      "\n",
      "| How would you describe your current GenAI/AI governance framework? | How would you describe your current GenAI/AI governance framework? | How would you describe your current GenAI/AI governance framework? | How would you describe your current GenAI/AI governance framework? | How would you describe your current GenAI/AI governance framework? |\n",
      "| ------------------------------------------------------------------ | ------------------------------------------------------------------ | ------------------------------------------------------------------ | ------------------------------------------------------------------ | ------------------------------------------------------------------ |\n",
      "| Artificial Intelligence (AI) Governance framework                  |                                                                    |                                                                    |                                                                    |                                                                    |\n",
      "| Well-established and comprehensive                                 | 13% (All respondents using/planning to use GenAI)                  | 33% (Using GenAI and have fully implemented it)                    | 18% (Using GenAI but haven't yet fully implemented it)             | 1% (Not yet using GenAI but intend to within the next two years)   |\n",
      "| In development                                                     | 61%                                                                | 64%                                                                | 69%                                                                | 52%                                                                |\n",
      "| Ad hoc or informal                                                 | 21%                                                                | 3%                                                                 | 13%                                                                | 34%                                                                |\n",
      "| Nonexistent                                                        | 6%                                                                 | 0%                                                                 | 0%                                                                 | 13%                                                                |\n",
      "| GenAI Governance framework                                         |                                                                    |                                                                    |                                                                    |                                                                    |\n",
      "| Well-established and comprehensive                                 | 5%                                                                 | 29%                                                                | 4%                                                                 | 0%                                                                 |\n",
      "| In development                                                     | 55%                                                                | 58%                                                                | 78%                                                                | 31%                                                                |\n",
      "| Ad hoc or informal                                                 | 28%                                                                | 13%                                                                | 17%                                                                | 43%                                                                |\n",
      "| Nonexistent                                                        | 11%                                                                | 0%                                                                 | 0%                                                                 | 26%                                                                |\n",
      "\n",
      "\n",
      "<ul>\n",
      "<li>All respondents using/planning to use GenAI</li>\n",
      "<li>Using GenAI and have fully implemented it</li>\n",
      "<li>Using GenAI but haven't yet fully implemented it</li>\n",
      "<li>Not yet using GenAI but intend to within the next two years</li>\n",
      "</ul>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Parse the GenAI Research Report\n",
    "print(\"Parsing GenAI Research Report with Agentic Mode...\")\n",
    "genai_result_agentic = await agentic_parser.aparse(genai_report_path)\n",
    "\n",
    "# Get markdown nodes\n",
    "genai_nodes_agentic = genai_result_agentic.get_markdown_nodes(split_by_page=True)\n",
    "print(f\"Number of pages extracted: {len(genai_nodes_agentic)}\")\n",
    "\n",
    "# Display sample output from page 7 (contains regulatory compliance data)\n",
    "print(\"\\n=== Sample Output - Page 7 (Agentic Mode) ===\")\n",
    "print(genai_nodes_agentic[6].text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Agentic Plus Mode\n",
    "\n",
    "The agentic plus mode (`parse_page_with_agent` with `anthropic-sonnet-4.0`) provides premium parsing for:\n",
    "- Highly complex documents with intricate layouts\n",
    "- Documents requiring maximum accuracy\n",
    "- Advanced reasoning over visual content\n",
    "- Critical business applications where quality is paramount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Agentic Plus Mode Parser initialized\n"
     ]
    }
   ],
   "source": [
    "# Initialize Agentic Plus Mode Parser\n",
    "agentic_plus_parser = LlamaParse(\n",
    "    parse_mode=\"parse_page_with_agent\",\n",
    "    model=\"anthropic-sonnet-4.0\",\n",
    "    high_res_ocr=True,\n",
    "    adaptive_long_table=True,\n",
    "    outlined_table_extraction=True,\n",
    "    output_tables_as_HTML=False,\n",
    "    result_type=\"markdown\",\n",
    "    project_id=project_id,\n",
    "    organization_id=organization_id,\n",
    ")\n",
    "\n",
    "print(\"Agentic Plus Mode Parser initialized\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parse Apple 10-K with Agentic Plus Mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parsing Apple 10-K with Agentic Plus Mode...\n",
      "Started parsing the file under job_id bfe790f5-a3c1-455d-8143-9403728772f4\n",
      "..Number of pages extracted: 82\n",
      "\n",
      "=== Sample Output - Page 32 (Agentic Plus Mode) ===\n",
      "\n",
      "# Apple Inc.\n",
      "\n",
      "## CONSOLIDATED STATEMENTS OF OPERATIONS\n",
      "*(In millions, except number of shares which are reflected in thousands and per share amounts)*\n",
      "\n",
      "|                                                  | Years ended<br/>September 25, 2021 | Years ended<br/>September 26, 2020 | Years ended<br/>September 28, 2019 |\n",
      "| ------------------------------------------------ | ---------------------------------- | ---------------------------------- | ---------------------------------- |\n",
      "| **Net sales:**                                   |                                    |                                    |                                    |\n",
      "| Products                                         | $ 297,392                          | $ 220,747                          | $ 213,883                          |\n",
      "| Services                                         | 68,425                             | 53,768                             | 46,291                             |\n",
      "| Total net sales                                  | 365,817                            | 274,515                            | 260,174                            |\n",
      "|                                                  |                                    |                                    |                                    |\n",
      "| **Cost of sales:**                               |                                    |                                    |                                    |\n",
      "| Products                                         | 192,266                            | 151,286                            | 144,996                            |\n",
      "| Services                                         | 20,715                             | 18,273                             | 16,786                             |\n",
      "| Total cost of sales                              | 212,981                            | 169,559                            | 161,782                            |\n",
      "| Gross margin                                     | 152,836                            | 104,956                            | 98,392                             |\n",
      "|                                                  |                                    |                                    |                                    |\n",
      "| **Operating expenses:**                          |                                    |                                    |                                    |\n",
      "| Research and development                         | 21,914                             | 18,752                             | 16,217                             |\n",
      "| Selling, general and administrative              | 21,973                             | 19,916                             | 18,245                             |\n",
      "| Total operating expenses                         | 43,887                             | 38,668                             | 34,462                             |\n",
      "|                                                  |                                    |                                    |                                    |\n",
      "| **Operating income**                             | 108,949                            | 66,288                             | 63,930                             |\n",
      "| **Other income/(expense), net**                  | 258                                | 803                                | 1,807                              |\n",
      "| **Income before provision for income taxes**     | 109,207                            | 67,091                             | 65,737                             |\n",
      "| **Provision for income taxes**                   | 14,527                             | 9,680                              | 10,481                             |\n",
      "| **Net income**                                   | $ 94,680                           | $ 57,411                           | $ 55,256                           |\n",
      "|                                                  |                                    |                                    |                                    |\n",
      "| **Earnings per share:**                          |                                    |                                    |                                    |\n",
      "| Basic                                            | $ 5.67                             | $ 3.31                             | $ 2.99                             |\n",
      "| Diluted                                          | $ 5.61                             | $ 3.28                             | $ 2.97                             |\n",
      "|                                                  |                                    |                                    |                                    |\n",
      "| **Shares used in computing earnings per share:** |                                    |                                    |                                    |\n",
      "| Basic                                            | 16,701,272                         | 17,352,119                         | 18,471,336                         |\n",
      "| Diluted                                          | 16,864,919                         | 17,528,214                         | 18,595,651                         |\n",
      "\n",
      "\n",
      "See accompanying Notes to Consolidated Financial Statements.\n",
      "\n",
      "Apple Inc. | 2021 Form 10-K | 29\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Parse the Apple 10-K document with premium mode\n",
    "print(\"Parsing Apple 10-K with Agentic Plus Mode...\")\n",
    "apple_result_agentic_plus = await agentic_plus_parser.aparse(apple_10k_path)\n",
    "\n",
    "# Get markdown nodes\n",
    "apple_nodes_agentic_plus = apple_result_agentic_plus.get_markdown_nodes(\n",
    "    split_by_page=True\n",
    ")\n",
    "print(f\"Number of pages extracted: {len(apple_nodes_agentic_plus)}\")\n",
    "\n",
    "# Display sample output from page 32\n",
    "print(\"\\n=== Sample Output - Page 32 (Agentic Plus Mode) ===\")\n",
    "print(apple_nodes_agentic_plus[31].text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question Answering Examples\n",
    "\n",
    "Now let's demonstrate how to use the parsed content to answer specific questions using an LLM."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import PromptTemplate\n",
    "\n",
    "\n",
    "async def ask_question_about_page(\n",
    "    page_content: str, question: str, document_type: str = \"document\"\n",
    ") -> str:\n",
    "    \"\"\"Helper function to ask questions about page content using LLM.\"\"\"\n",
    "    qa_template = PromptTemplate(\n",
    "        \"\"\"\n",
    "        Based on the following page content from a {document_type}, please answer the question:\n",
    "\n",
    "        Question: {question}\n",
    "\n",
    "        Page Content:\n",
    "        {page_content}\n",
    "\n",
    "        Please provide a specific answer with numbers if available.\n",
    "        \"\"\"\n",
    "    )\n",
    "\n",
    "    prompt = qa_template.format(\n",
    "        question=question, page_content=page_content, document_type=document_type\n",
    "    )\n",
    "\n",
    "    response = await llm.acomplete(prompt)\n",
    "    return response.text"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 1: Apple 10-K Financial Data\n",
    "\n",
    "**Question**: \"What are net sales in Q3 September 2021 including product/services breakdown?\"\n",
    "\n",
    "**Source**: Page 32 of Apple 10-K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Apple 10-K Financial Data Answer ===\n",
      "For the period shown (year ended September 25, 2021) net sales were (in millions):\n",
      "\n",
      "- Products: $297,392\n",
      "- Services: $68,425\n",
      "- Total net sales: $365,817\n"
     ]
    }
   ],
   "source": [
    "# Use the cost-effective mode result for this example\n",
    "page_32_content = apple_nodes_cost_effective[31].text\n",
    "question = (\n",
    "    \"What are net sales in Q3 September 2021 including product/services breakdown?\"\n",
    ")\n",
    "\n",
    "answer = await ask_question_about_page(\n",
    "    page_content=page_32_content, question=question, document_type=\"Apple's 10-K filing\"\n",
    ")\n",
    "\n",
    "print(\"=== Apple 10-K Financial Data Answer ===\")\n",
    "print(answer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 2: GenAI Research Report Compliance\n",
    "\n",
    "**Question**: \"How prepared are organizations in complying with current/upcoming regulations concerning genAI?\"\n",
    "\n",
    "**Source**: Page 7 of GenAI Research Report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== GenAI Regulatory Compliance Answer ===\n",
      "Short answer: organizations are largely unprepared — only 1 in 10 fully prepared overall.\n",
      "\n",
      "Details (from the report)\n",
      "\n",
      "Compliance preparedness (current & upcoming GenAI regulations)\n",
      "- All respondents using / planning to use GenAI\n",
      "  - Fully prepared: 10%\n",
      "  - Moderately prepared: 48%\n",
      "  - Slightly prepared: 40%\n",
      "  - Not prepared: 2%\n",
      "- Using GenAI and have fully implemented it\n",
      "  - Fully prepared: 35%\n",
      "  - Moderately prepared: 49%\n",
      "  - Slightly prepared: 15%\n",
      "  - Not prepared: 0%\n",
      "- Using GenAI but not yet fully implemented\n",
      "  - Fully prepared: 11%\n",
      "  - Moderately prepared: 66%\n",
      "  - Slightly prepared: 23%\n",
      "  - Not prepared: 0%\n",
      "- Not yet using GenAI but intend to within two years\n",
      "  - Fully prepared: 3%\n",
      "  - Moderately prepared: 28%\n",
      "  - Slightly prepared: 64%\n",
      "  - Not prepared: 5%\n",
      "\n",
      "Governance context (AI / GenAI frameworks)\n",
      "- GenAI governance (all respondents)\n",
      "  - Well‑established & comprehensive: 5%\n",
      "  - In development: 55%\n",
      "  - Ad hoc/informal: 28%\n",
      "  - Nonexistent: 11%\n",
      "- GenAI governance (using GenAI and fully implemented)\n",
      "  - Well‑established: 29% (so 71% do not have a well‑established GenAI framework)\n",
      "  - In development: 58%\n",
      "  - Ad hoc: 13%\n",
      "  - Nonexistent: 0%\n",
      "\n",
      "Interpretation: only 10% of organizations overall say they are fully prepared to comply; most are moderately (48%) or slightly (40%) prepared. GenAI governance is rarely comprehensive (5% overall), and even among fully implemented users only 29% have a well‑established GenAI governance framework — meaning roughly 7 in 10 adopters still lack a comprehensive framework.\n"
     ]
    }
   ],
   "source": [
    "# Use the agentic mode result for this example\n",
    "page_7_content = genai_nodes_agentic[6].text\n",
    "question = \"How prepared are organizations in complying with current/upcoming regulations concerning genAI?\"\n",
    "\n",
    "answer = await ask_question_about_page(\n",
    "    page_content=page_7_content,\n",
    "    question=question,\n",
    "    document_type=\"GenAI Research Report\",\n",
    ")\n",
    "\n",
    "print(\"=== GenAI Regulatory Compliance Answer ===\")\n",
    "print(answer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EU Server Configuration\n",
    "\n",
    "For users in Europe or those requiring EU data residency, you can easily configure LlamaParse to use the EU server by adding the `base_url` parameter.\n",
    "\n",
    "**NOTE**: You will need to sign up for an account on https://cloud.eu.llamaindex.ai/ and get a separate API key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example: EU Server Configuration\n",
    "eu_parser = LlamaParse(\n",
    "    parse_mode=\"parse_page_with_agent\",\n",
    "    model=\"openai-gpt-4-1-mini\",\n",
    "    base_url=\"https://api.cloud.eu.llamaindex.ai\",  # EU server endpoint\n",
    "    high_res_ocr=True,\n",
    "    adaptive_long_table=True,\n",
    "    outlined_table_extraction=True,\n",
    "    output_tables_as_HTML=False,\n",
    "    result_type=\"markdown\",\n",
    "    project_id=project_id,\n",
    "    organization_id=organization_id,\n",
    "    api_key=\"<llamacloud_eu_api_key>\",\n",
    ")\n",
    "\n",
    "print(\"EU Server Parser configured (not executed in this demo)\")\n",
    "print(\"Simply add base_url='https://api.cloud.eu.llamaindex.ai' to use EU servers\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mode Comparison Summary\n",
    "\n",
    "| Mode | Use Case | Cost | Speed | Accuracy |\n",
    "|------|----------|------|-------|----------|\n",
    "| **Cost-Effective** | High-volume, text-heavy documents | ⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐ |\n",
    "| **Agentic (Default)** | General purpose, balanced performance | ⭐⭐ | ⭐⭐ | ⭐⭐⭐ |\n",
    "| **Agentic Plus** | Complex documents, maximum accuracy | ⭐ | ⭐ | ⭐⭐⭐ |\n",
    "\n",
    "### Choosing the Right Mode:\n",
    "\n",
    "- **Start with Agentic Mode** - It's the default for good reason, offering the best balance of quality and cost\n",
    "- **Use Cost-Effective Mode** when processing large volumes of straightforward documents\n",
    "- **Upgrade to Agentic Plus Mode** for complex documents with intricate layouts, charts, or when maximum accuracy is required"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Next Steps and Additional Resources\n",
    "\n",
    "Now that you've learned about LlamaParse's different modes, explore these resources for deeper dives:\n",
    "\n",
    "### Advanced Features\n",
    "- **JSON Mode Analysis**: Check out `demo_json_tour.ipynb` for detailed analysis of parsing outputs through JSON mode\n",
    "- **Auto Mode**: Explore `parsing_modes/demo_auto_mode.ipynb` for automatic mode selection based on document characteristics\n",
    "\n",
    "### Building Applications\n",
    "- **LlamaCloud Getting Started**: To setup an e2e RAG/retrieval pipeline, visit the [LlamaCloud Getting Started Guide](https://docs.cloud.llamaindex.ai/llamacloud/how_to/getting-started-with-index)\n",
    "- **API Documentation**: Full API reference at [LlamaCloud Documentation](https://docs.cloud.llamaindex.ai/API/llama-platform)\n",
    "\n",
    "### Key Configuration Options\n",
    "- `high_res_ocr=True` - Enhanced OCR for better text extraction\n",
    "- `adaptive_long_table=True` - Better handling of complex tables\n",
    "- `outlined_table_extraction=True` - Improved table structure detection\n",
    "- `output_tables_as_HTML=False` - Output tables as markdown instead of HTML\n",
    "- `result_type=\"markdown\"` - Clean, structured output format\n",
    "\n",
    "Happy parsing! 🚀"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_parse",
   "language": "python",
   "name": "llama_parse"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
